home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / source / shell / split.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-25  |  5.1 KB  |  207 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. #include <stdio.h>
  3. #include "shell.h"
  4.  
  5. #define MAX_ENV 50
  6.  
  7. #define HEAD "/*$DINO"
  8. #define FILES "/*$DINO FILES"
  9. #define NOFILES "/*$DINO NOFILES"
  10. #define SETFILES "/*$DINO SET FILES"
  11. #define RESET "/*$DINO RESET FILES"
  12. #define ONEFILE "/*$DINO FILE"
  13. #define ALL "/*$DINO ALL FILES"
  14.  
  15. #define IS_CHAR(c) ((('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z')))
  16. #define IS_CHAR_NUM(c) (IS_CHAR(c) || (('0' <= c) && (c <= '9')))
  17.  
  18. struct{
  19.         char name[MAX_STRING];
  20.         int len;
  21.         FILE *fd;
  22.         int all;
  23.         int current;}
  24.     table[MAX_ENV];
  25. int next_table = 0;
  26.  
  27. char suffix[MAX_STRING] = {'D','\0'};
  28. char root[MAX_STRING];
  29.  
  30. static BOOL model = FALSE;
  31.  
  32. init(argc, argv)
  33.     int argc;
  34.     char *argv[];
  35.     {
  36.     int in;
  37.  
  38.     if (strcmp(argv[1], "new") == 0)
  39.         model = TRUE;
  40.     (void) strcpy(root, argv[2]);
  41.  
  42.     for (in = 3; in < argc; in++)
  43.         if (argv[in][0] == '-') 
  44.             switch(argv[in][1])
  45.                 {
  46.                 case 's' : 
  47.                     if (strlen(argv[in]) >2) 
  48.                         strcpy(suffix, argv[in] +2);
  49.                     else strcpy(suffix,"");
  50.                     break;
  51.                 default : ;
  52.                 };
  53.     }
  54. char *get_name(pt)
  55.     char **pt;
  56.     {
  57.     static char name[MAX_STRING];
  58.     char *nm = name;
  59.     int i;
  60.     char *p = *pt;
  61.     register char c = *p;
  62.  
  63.     name[0] = '\0';
  64.     c = *p;
  65.     if (!IS_CHAR(c))
  66.         while (c = *(++p), !IS_CHAR(c) && (c!='\n'));
  67.     if (c=='\n') return name;
  68.     do 
  69.         *(nm++) = c;
  70.         while (c = *(++p), IS_CHAR_NUM(c) && (c!='\n'));
  71.     *nm = '\0';
  72.     *pt = p;
  73.     return name;
  74.     }
  75.  
  76. load_table(pt)
  77.     char *pt;
  78.     {
  79.     char *nm;
  80.     do {
  81.         nm = get_name(&pt);
  82.         if (*nm != '\0'){
  83.             char file[MAX_STRING];
  84.  
  85.             strcpy(table[next_table].name, nm);
  86.             table[next_table].len = strlen(nm);
  87.             if (model)
  88.                 (void) sprintf(file, "%s.%s.%s.c", root, suffix, nm);
  89.             else
  90.                 (void) sprintf(file, "%s%s.c", nm, suffix);
  91.             table[next_table].fd = fopen(file, "w");
  92.             if (table[next_table].fd == NULL)
  93.                {
  94.                 perror("\tShell");
  95.                 (void) fprintf(stderr,
  96.                         "\t\tUnable to open %s for writing.\n", file);
  97.  
  98.                 exit(1);
  99.                }
  100.             table[next_table].current = table[next_table].all = 1;
  101.             next_table++;
  102.             if (next_table >= MAX_ENV)
  103.                 fprintf(stderr,"file splitter: too many environment structures");
  104.             }
  105.         } while (*nm != '\0');
  106.     }
  107.  
  108. set_files(pt)
  109.     char *pt;
  110.     {
  111.     int tbl;
  112.     int not_fnd;
  113.     char *nm;
  114.  
  115.     for (tbl=0; tbl<next_table; tbl++)
  116.         table[tbl].all = table[tbl].current = 0;
  117.     do {
  118.         nm = get_name(&pt);
  119.         if (*nm != '\0'){
  120.             tbl=0;
  121.             not_fnd = 1;
  122.             while ((tbl<next_table) && (not_fnd)) {
  123.                 if (table[tbl].len != strlen(nm))
  124.                     not_fnd = 1;
  125.                     else not_fnd = 
  126.                         (0 != strncmp(nm, table[tbl].name, table[tbl].len));
  127.                 tbl++;
  128.                 }
  129.             tbl--;
  130.             if (not_fnd == 0)
  131.                 table[tbl].all = table[tbl].current = 1;
  132.             }
  133.         } while (*nm != '\0');
  134.     }
  135.  
  136. current_file(pt)
  137.     char *pt;
  138.     {
  139.     int tbl;
  140.     int not_fnd;
  141.     char *nm;
  142.  
  143.     for (tbl=0; tbl<next_table; tbl++)
  144.         table[tbl].current = 0;
  145.     nm = get_name(&pt);
  146.     if (*nm != '\0'){
  147.         tbl=0;
  148.         not_fnd = 1;
  149.         while ((tbl<next_table) && (not_fnd)){
  150.             if (table[tbl].len != strlen(nm))
  151.                 not_fnd = 1;
  152.                 else not_fnd = 
  153.                     (0 != strncmp(nm, table[tbl].name, table[tbl].len)); 
  154.             tbl++;
  155.             }
  156.         tbl--;
  157.         if (not_fnd == 0)
  158.             table[tbl].current = 1;
  159.         }
  160.     }
  161.  
  162. split(pt)
  163.     char *pt;
  164.     {
  165.     int tbl;
  166.  
  167.     if (strncmp(HEAD,pt,7) == 0)
  168.         {
  169.             if (strncmp(FILES,pt,strlen(FILES)) == 0) 
  170.                 load_table(pt + strlen(FILES));
  171.             else if (strncmp(NOFILES,pt,strlen(NOFILES)) == 0) 
  172.                 exit(0);
  173.             else if (strncmp(SETFILES,pt,strlen(SETFILES)) == 0) 
  174.                 set_files(pt + strlen(SETFILES));
  175.             else if (strncmp(RESET,pt,strlen(RESET)) == 0)
  176.                 for (tbl = 0; tbl < next_table; tbl++)
  177.                     table[tbl].all = table[tbl].current = 1;
  178.             else if (strncmp(ONEFILE,pt,strlen(ONEFILE)) == 0) 
  179.                 current_file(pt + strlen(ONEFILE));
  180.             else if (strncmp(ALL,pt,strlen(ALL)) == 0)
  181.                 for (tbl = 0; tbl < next_table; tbl++)
  182.                     table[tbl].current = table[tbl].all;
  183.             }
  184.         else 
  185.             for(tbl = 0; tbl < next_table; tbl++)
  186.                 if(table[tbl].current){
  187.                     fprintf(table[tbl].fd, "%s", pt);
  188.                     /*printf("%s: %s",table[tbl].name, pt);*/}
  189.     }
  190.  
  191. main(argc,argv)
  192.     int argc;
  193.     char *argv[];
  194.     {
  195.     int done = 0;
  196.     char *line;
  197.  
  198.     init(argc,argv);
  199.     while(!done){
  200.         line = get_line(0);
  201.         if (*line != '\0')
  202.             split(line);
  203.             else done = 1;
  204.         }
  205.     return(0);
  206.     }
  207.